cat('Creating Tables A7 and A8 \n\n')


dat = read.csv('TablesA7A8_data.csv')

non.south.dat = dat[dat$south == 0,]

reg1.out = lm(white.obama.vote~dissimilarity+cbsa.percent.black+kerry.pct.04+Precinct.State,
             data = dat,
             weight = dat$total_population)

reg2.out = lm(white.obama.vote~dissimilarity*cbsa.percent.black+kerry.pct.04+Precinct.State,
              data = dat,
              weight = dat$total_population)

reg3.out = lm(white.obama.vote~dissimilarity+cbsa.percent.black+I(cbsa.percent.black^2)+kerry.pct.04+Precinct.State,
              data = dat,
              weight = dat$total_population)

reg4.out = lm(white.obama.vote~dissimilarity*cbsa.percent.black+dissimilarity*I(cbsa.percent.black^2)+kerry.pct.04+Precinct.State,
              data = dat,
              weight = dat$total_population)

reg5.out = lm(white.obama.vote~dissimilarity+cbsa.percent.black+pct.black+kerry.pct.04+hhi_white_nonhisp+
+Precinct.State,
              data = dat,
              weight = dat$total_population)

reg6.out = lm(white.obama.vote~dissimilarity*cbsa.percent.black+pct.black+kerry.pct.04+hhi_white_nonhisp+
+Precinct.State,
              data = dat,
              weight = dat$total_population)

reg7.out = lm(white.obama.vote~dissimilarity+cbsa.percent.black+I(cbsa.percent.black^2)+pct.black+kerry.pct.04+hhi_white_nonhisp+
+Precinct.State,
              data = dat,
              weight = dat$total_population)

reg8.out = lm(white.obama.vote~dissimilarity*cbsa.percent.black+dissimilarity*I(cbsa.percent.black^2)+pct.black+kerry.pct.04+hhi_white_nonhisp+
+Precinct.State,
              data = dat,
              weight = dat$total_population)


coef.names = c('Intercept', 'Segregation', 'Black Population',  'Segregation x Black Population', 'Black Population\verb|^|2','Segregation x Black Population\verb|^|2','Income','College Educated')

outtable = apsrtable(reg1.out,
                     reg2.out,
                     reg3.out,
                     reg4.out,
                      reg5.out,
                     reg6.out,
                     reg7.out,
                     reg8.out,
                     Sweave = T,
                     omitcoef = 'Precinct.State',
 #                    coef.names = coef.names,
                     #model.names = c('UO','Non UO','UO','Non UO','UO','Non UO'),
                     notes = '',
                     stars = 'default'
)
writeLines(
  outtable, 'TableA7.tex')





reg1.out = lm(white.obama.vote~dissimilarity+cbsa.percent.black+kerry.pct.04+Precinct.State,
              data = non.south.dat,
              weight = non.south.dat$total_population)

reg2.out = lm(white.obama.vote~dissimilarity*cbsa.percent.black+kerry.pct.04+Precinct.State,
              data = non.south.dat,
              weight = non.south.dat$total_population)

reg3.out = lm(white.obama.vote~dissimilarity+cbsa.percent.black+I(cbsa.percent.black^2)+kerry.pct.04+Precinct.State,
              data = non.south.dat,
              weight = non.south.dat$total_population)

reg4.out = lm(white.obama.vote~dissimilarity*cbsa.percent.black+dissimilarity*I(cbsa.percent.black^2)+kerry.pct.04+Precinct.State,
              data = non.south.dat,
              weight = non.south.dat$total_population)

reg5.out = lm(white.obama.vote~dissimilarity+cbsa.percent.black+pct.black+kerry.pct.04+hhi_white_nonhisp+
                +Precinct.State,
              data = non.south.dat,
              weight = non.south.dat$total_population)

reg6.out = lm(white.obama.vote~dissimilarity*cbsa.percent.black+pct.black+kerry.pct.04+hhi_white_nonhisp+
                +Precinct.State,
              data = non.south.dat,
              weight = non.south.dat$total_population)

reg7.out = lm(white.obama.vote~dissimilarity+cbsa.percent.black+I(cbsa.percent.black^2)+pct.black+kerry.pct.04+hhi_white_nonhisp+
                +Precinct.State,
              data = non.south.dat,
              weight = non.south.dat$total_population)

reg8.out = lm(white.obama.vote~dissimilarity*cbsa.percent.black+dissimilarity*I(cbsa.percent.black^2)+pct.black+kerry.pct.04+hhi_white_nonhisp+
                +Precinct.State,
              data = non.south.dat,
              weight = non.south.dat$total_population)



outtable = apsrtable(reg1.out,
                     reg2.out,
                     reg3.out,
                     reg4.out,
                      reg5.out,
                     reg6.out,
                     reg7.out,
                     reg8.out,
                     Sweave = T,
                     omitcoef = 'Precinct.State',
                     #                    coef.names = coef.names,
                     #model.names = c('UO','Non UO','UO','Non UO','UO','Non UO'),
                     notes = '',
                     stars = 'default'
                    )
writeLines(
  outtable, 'TableA8.tex')
